﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Shared;

namespace DAL
{
    public abstract class BaseRepository<T>: IRepository<T> where T :class
    {
        protected readonly IdbContext _context;

        public BaseRepository(IdbContext context)
        {
            if (context != null)
            {
                _context = context;
            }
            else throw new ArgumentNullException("context");
        }

        public virtual void Add(T entry)
        {
            _context.Set<T>().Add(entry);
            _context.SaveChanges();
        }

        public virtual void Delete(T entry)
        {
            _context.Entry(entry).State = System.Data.Entity.EntityState.Deleted;
            _context.SaveChanges();
        }

        public virtual T GetById(int id)
        {
            return _context.Set<T>().Find(id);          
        }

        public virtual void Update(T entry)
        {
            _context.Entry(entry).State = System.Data.Entity.EntityState.Modified;
            _context.SaveChanges();
        }
    }
}
